package me.chanjar.weixin.mp.api.impl;

import com.platform.common.utils.Constant;
import me.chanjar.weixin.common.bean.WxOAuth2UserInfo;
import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
import me.chanjar.weixin.common.enums.WxType;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.error.WxRuntimeException;
import me.chanjar.weixin.common.service.WxOAuth2Service;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
import me.chanjar.weixin.common.util.http.URIUtil;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.enums.WxComponentApiUrl;
import me.chanjar.weixin.mp.enums.WxMpApiUrl;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;

import java.io.IOException;

public class WxMpComponentOAuth2ServiceImpl implements WxOAuth2Service {
    private final AbstractComponentWxMpServiceImpl wxMpService;
    private static final Logger log = LoggerFactory.getLogger(WxMpComponentOAuth2ServiceImpl.class);

    @Override
    public String buildAuthorizationUrl(String redirectUri, String scope, String state) {
        return String.format(WxMpApiUrl.OAuth2.CONNECT_OAUTH2_AUTHORIZE_URL.getUrl(this.getMpConfigStorage()), this.getMpConfigStorage().getAppId(), URIUtil.encodeURIComponent(redirectUri), scope, StringUtils.trimToEmpty(state));
    }

    private WxOAuth2AccessToken getOAuth2AccessToken(String url) throws WxErrorException {
        try {
            RequestExecutor<String, String> executor = SimpleGetRequestExecutor.create(this.wxMpService.getRequestHttp());
            String responseText = (String) executor.execute(url, null, WxType.MP);
            return WxOAuth2AccessToken.fromJson(responseText);
        } catch (IOException var4) {
            throw new WxErrorException(WxError.builder().errorCode(99999).errorMsg(var4.getMessage()).build(), var4);
        }
    }

    @Override
    public WxOAuth2AccessToken getAccessToken(String code) throws WxErrorException {
        String componentAppid, accessToken;
        WxMpConfigStorage config = wxMpService.getWxMpConfigStorage();
        Jedis jedis = ((ComponentWxMpRedisConfigImpl) config).getJedisPool().getResource();
        componentAppid = jedis.get(Constant.COMPONENT_APPID);
        accessToken = wxMpService.getComponentAccessToken();
        log.info("componentAppid:" + componentAppid);
        log.info("accessToken:" + accessToken);
        String url = String.format(WxComponentApiUrl.Component.OAUTH2_ACCESS_TOKEN_URL.getUrl(wxMpService.getWxMpConfigStorage()), wxMpService.getWxMpConfigStorage().getAppId(), code, componentAppid, accessToken);
        jedis.close();
        return this.getOAuth2AccessToken(url);
    }

    @Override
    public WxOAuth2AccessToken getAccessToken(String appId, String appSecret, String code) throws WxErrorException {
        return this.getOAuth2AccessToken(String.format(WxMpApiUrl.OAuth2.OAUTH2_ACCESS_TOKEN_URL.getUrl(this.getMpConfigStorage()), appId, appSecret, code));
    }

    @Override
    public WxOAuth2AccessToken refreshAccessToken(String refreshToken) throws WxErrorException {
        String url = String.format(WxMpApiUrl.OAuth2.OAUTH2_REFRESH_TOKEN_URL.getUrl(this.getMpConfigStorage()), this.getMpConfigStorage().getAppId(), refreshToken);
        return this.getOAuth2AccessToken(url);
    }

    protected WxMpConfigStorage getMpConfigStorage() {
        return this.wxMpService.getWxMpConfigStorage();
    }

    @Override
    public WxOAuth2UserInfo getUserInfo(WxOAuth2AccessToken token, String lang) throws WxErrorException {
        if (lang == null) {
            lang = "zh_CN";
        }

        String url = String.format(WxMpApiUrl.OAuth2.OAUTH2_USERINFO_URL.getUrl(this.getMpConfigStorage()), token.getAccessToken(), token.getOpenId(), lang);

        try {
            RequestExecutor<String, String> executor = SimpleGetRequestExecutor.create(this.wxMpService.getRequestHttp());
            String responseText = (String) executor.execute(url, null, WxType.MP);
            return WxOAuth2UserInfo.fromJson(responseText);
        } catch (IOException var6) {
            throw new WxRuntimeException(var6);
        }
    }

    @Override
    public boolean validateAccessToken(WxOAuth2AccessToken token) {
        String url = String.format(WxMpApiUrl.OAuth2.OAUTH2_VALIDATE_TOKEN_URL.getUrl(this.getMpConfigStorage()), token.getAccessToken(), token.getOpenId());

        try {
            SimpleGetRequestExecutor.create(this.wxMpService.getRequestHttp()).execute(url, null, WxType.MP);
            return true;
        } catch (IOException var4) {
            throw new WxRuntimeException(var4);
        } catch (WxErrorException var5) {
            return false;
        }
    }

    public WxMpComponentOAuth2ServiceImpl(AbstractComponentWxMpServiceImpl wxMpService) {
        this.wxMpService = wxMpService;
    }
}
